/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.company;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jobportal.Dbconn;
import jobportal.EmailSender;

/**
 *
 * @author project
 */
public class Result extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        RequestDispatcher rd;
        rd = request.getRequestDispatcher("result.jsp");
        try {
            HttpSession session = request.getSession();
            Connection con = Dbconn.getConn();
            Statement st = con.createStatement();
            String jobid="", username="", sql="", result="",email="";
            jobid = request.getParameter("jobid");
            username = request.getParameter("username");


            result = request.getParameter("result");
            sql = "update job_applied set result='" + result + "' where jobid='" + jobid + "' and username='" + username + "'";
            if (st.executeUpdate(sql) > 0) {
                ResultSet rs = st.executeQuery("select name,jobtitle,compname,student.email from student,jobs,job_applied,company where student.username=job_applied.username and job_applied.jobid=jobs.jobid and jobs.compid=company.compid and jobs.jobid='" + jobid + "' and student.username='" + username + "'");
                String name = "", compname = "", jobtitle = "";
                if (rs.next()) {
                    name = rs.getString(1);
                    jobtitle = rs.getString(2);
                    compname = rs.getString(3);
                    email=rs.getString(4);
                }
                rs=st.executeQuery("select subject,content from emailmessage where id=4");
                String subject="",content="";
                if(rs.next()){
                    subject=rs.getString(1);
                    content=rs.getString(2);
                }
                content=content.replace("%stuname%",name);
                content=content.replace("%compname%",compname);
                content=content.replace("%jobtitle%", jobtitle);
                EmailSender.sendMail(email, subject, content);
            }

            rd.forward(request, response);
        } catch (Exception e) {
            out.println(e);

        }
    }
}
